//
//  JsUnitManager.m
//  JsUtilitySdk
//
//  Created by Gikki Ares on 2023/4/24.
//

#import "JsUnitManager.h"

@implementation JsUnitManager

+(NSArray *)suitableTimeDescForSec:(float)s {
	float m = s/60;
	if(m < 1) {
		return @[@(s),@"s"];
	}
	float h = m/60;
	if(h < 1) {
		return @[@(m),@"m"];
	}
	float d = h/24;
	if(d < 1) {
		return @[@(h),@"h"];
	}
	else {
		return @[@(d),@"d"];
	}
}

+ (NSArray *)suitableByteDescForByteSize:(float)B {
	float KB = B/1024;
	if(KB < 1) {
		return @[@(B),@"B"];
	}
	float MB = KB/1024;
	if(MB < 1) {
		return @[@(KB),@"KB"];
	}
	float GB = MB/1024;
	if(GB < 1) {
		return @[@(MB),@"MB"];
	}
	float TB = GB/1024;
	if(TB < 1) {
		return @[@(GB),@"GB"];
	}
	float PB = TB/1024;
	if(PB < 1) {
		return @[@(TB),@"TB"];
	}
	else {
		return @[@(PB),@"PB"];
	}
}

+ (NSString *)suitableByteDescStringForByteSize:(float)B {
	NSArray * arr = [self suitableByteDescForByteSize:B];
	float value = [arr[0] floatValue];
	NSString * unit = arr[1];
	return [NSString stringWithFormat:@"%.2f %@",value,unit];
}
+ (NSString *)suitableBitDescStringForByteSize:(float)B {
	NSArray * arr = [self suitableBitDescForByteSize:B];
	float value = [arr[0] floatValue];
	NSString * unit = arr[1];
	return [NSString stringWithFormat:@"%.2f %@",value,unit];
}

+ (NSArray *)suitableBitDescForByteSize:(float)B {
	float b = B * 8;
	float Kb = b/1024;
	if(Kb < 1) {
		return @[@(b),@"b"];
	}
	float Mb = Kb/1024;
	if(Mb < 1) {
		return @[@(Kb),@"Kb"];
	}
	float Gb = Mb/1024;
	if(Gb < 1) {
		return @[@(Mb),@"Mb"];
	}
	float Tb = Gb/1024;
	if(Tb < 1) {
		return @[@(Gb),@"Gb"];
	}
	float Pb = Tb/1024;
	if(Pb < 1) {
		return @[@(Tb),@"Tb"];
	}
	else {
		return @[@(Pb),@"Pb"];
	}
}

+(NSString *)suitableByteSpeedStringForByteDeltaSize:(float)B {
	NSArray * arr = [self suitableByteDescForByteSize:B];
	return [NSString stringWithFormat:@"%.2f %@/s",[arr[0] floatValue],arr[1]];
}

+(NSString *)suitableBitSpeedStringForByteDeltaSize:(float)B {
	NSArray * arr = [self suitableBitDescForByteSize:B];
	return [NSString stringWithFormat:@"%.2f %@/s",[arr[0] floatValue],arr[1]];
}

@end
